package november.tree.binaryTree;

/**
 * @author huangxin
 * @date 2019/11/20-22:20
 */
public class BinaryTree {
    public static void main(String[] args) {
        BinaryTree tree = new BinaryTree();


        TreeNode root = new TreeNode(1, "root");
        root.left = new TreeNode(2, "abc");
        root.right = new TreeNode(3, "bcd");
        root.right.left = new TreeNode(5, "bcd");
        root.right.right = new TreeNode(4, "bcd");


        tree.root = root;

        tree.afterOrder();
        tree.delete(5);
        System.out.println();
        tree.afterOrder();


    }


    public TreeNode root;

    //前序
    public void preOrder() {
        if (this.root == null) {
            System.out.println("二叉树为空");
            return;
        }
        this.root.preOrder();
    }

    //中序
    public void infixOrder() {
        if (this.root == null) {
            System.out.println("二叉树为空");
            return;
        }
        this.root.infixOrder();
    }

    //后序
    public void afterOrder() {
        if (this.root == null) {
            System.out.println("二叉树为空");
            return;
        }
        this.root.afterOrder();
    }


    //前序查找
    public TreeNode preSearch(int id) {
        if (this.root == null) {
            System.out.println("二叉树为空");
            return null;
        }
        return this.root.preSearch(id);
    }

    //中序查找
    public TreeNode infixSearch(int id) {
        if (this.root == null) {
            System.out.println("二叉树为空");
            return null;
        }
        return this.root.infixSearch(id);
    }

    //后序查找
    public TreeNode afterSearch(int id) {
        if (this.root == null) {
            System.out.println("二叉树为空");
            return null;
        }
        return this.root.afterSearch(id);
    }


    public void delete(int id) {
        if (root != null) {
            //判断root节点
            if (root.id == id) {
                root = null;
            }
            root.delete(id);
        } else {
            System.err.println("无法删除空树");
        }
    }

}
